package OurGame;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.Console;
import java.util.ArrayList;

import javax.swing.*;

public class Board extends JPanel implements ActionListener, Runnable {
	Dude p;
	Dude2 p2;
	Dude3 p3;
	Ro ro;
	Image img;
	Timer time;

	int v = 10;
	int vRo = 565;
	Thread animator;
	Thread animator2;
	Thread animator3;

	static Font font = new Font("SanSerif", Font.BOLD, 24);

	boolean a = false;
	boolean done2 = false;
	boolean lost = false;

	public Board() {
		p = new Dude(0);
		p2 = new Dude2(500 / 2);
		p3 = new Dude3((500 - 90));
		ro = new Ro();
		// addKeyListener(new AL());
		addKeyListener(new KeyListener() {

			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub

			}

			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				ro.keyReleased(e);
			}

			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				ro.keyPressed(e);
			}
		});
		setFocusable(true);
		ImageIcon i = new ImageIcon("Image/bg2.jpg");
		img = i.getImage();
		time = new Timer(6, this);
		time.start();
	}

	public void actionPerformed(ActionEvent e) {
		checkCollisions();
		ArrayList bullets = Dude.getBullets();
		for (int w = 0; w < bullets.size(); w++) {
			// This is how to get a current element in an arrayList
			// similar to x[2] in a normal array
			Bullet m = (Bullet) bullets.get(w);// draw:
			if (m.getVisible() == true) {
				m.move();
			} else
				bullets.remove(w);
		}

		ArrayList bullets2 = Dude2.getBullets();
		for (int w = 0; w < bullets2.size(); w++) {
			// This is how to get a current element in an arrayList
			// similar to x[2] in a normal array
			Bullet m = (Bullet) bullets2.get(w);// draw:
			if (m.getVisible() == true) {
				m.move();
			} else
				bullets2.remove(w);
		}

		ArrayList bullets3 = Dude3.getBullets();
		for (int w = 0; w < bullets3.size(); w++) {
			// This is how to get a current element in an arrayList
			// similar to x[2] in a normal array
			Bullet m = (Bullet) bullets3.get(w);// draw:
			if (m.getVisible() == true) {
				m.move();
			} else
				bullets3.remove(w);
		}

		p.move();
		p2.move();
		p3.move();
		ro.move();
		repaint();
	}

	public void paint(Graphics g) {
		if (p.dy == 1 && done2 == false) {
			done2 = true;
			animator = new Thread(this);
			animator.start();
		}
		p.y = v;
		// 2
		// if (p2.dy == 1 && done2 == false) {
		// done2 = true;
		// animator2 = new Thread(this);
		// animator2.start();
		// }
		p2.y = v;
		// 3
		// if (p3.dy == 1 && done2 == false) {
		// done2 = true;
		// animator3 = new Thread(this);
		// animator3.start();
		// }
		p3.y = v;
		// ro
		// if (ro.dy == 1 && done2 == false) {
		// done2 = true;
		// animator3 = new Thread(this);
		// animator3.start();
		// }
		ro.y = vRo;

		super.paint(g);
		Graphics2D g2d = (Graphics2D) g;

		// Get bullets from ArrayList

		if ((p.getX() - 590) % 2400 == 0)// p.getX() == 590 || p.getX() == 2990)
			p.nx = 0;
		if ((p.getX() - 1790) % 2400 == 0)// p.getX() == 1790 || p.getX() ==
											// 4190)
			p.nx2 = 0;

		g2d.drawImage(img, 685 - p.getnX2(), 0, null);
		if (p.getX() > 590) {
			g2d.drawImage(img, 685 - p.getnX(), 0, null);
		}

		g2d.drawImage(p.getImage(), p.left, v, null);

		if (p.getdx() == -1) {

			g2d.drawImage(img, 685 - p.getnX2(), 0, null);
			g2d.drawImage(p.getImage(), p.left, v, null);
		}

		// //p2
		// if ((p2.getX() - 590) % 2400 == 0)// p.getX() == 590 || p.getX() ==
		// // 2990)
		// p2.nx = 0;
		// if ((p2.getX() - 1790) % 2400 == 0)// p.getX() == 1790 || p.getX() ==
		// // 4190)
		// p2.nx2 = 0;
		//
		// g2d.drawImage(img, 685 - p2.getnX2(), 0, null);
		// if (p2.getX() > 590) {
		// g2d.drawImage(img, 685 - p2.getnX(), 0, null);
		// }
		//
		g2d.drawImage(p2.getImage(), p2.left, v, null);
		//
		// if (p2.getdx() == -1) {
		//
		// g2d.drawImage(img, 685 - p2.getnX2(), 0, null);
		// g2d.drawImage(p2.getImage(), p2.left, v, null);
		// }
		// // //p3
		// if ((p3.getX() - 590) % 2400 == 0)// p.getX() == 590 || p.getX() ==
		// // 2990)
		// p3.nx = 0;
		// if ((p3.getX() - 1790) % 2400 == 0)// p.getX() == 1790 || p.getX() ==
		// // 4190)
		// p2.nx2 = 0;
		//
		// g2d.drawImage(img, 685 - p3.getnX2(), 0, null);
		// if (p3.getX() > 590) {
		// g2d.drawImage(img, 685 - p3.getnX(), 0, null);
		// }
		//
		// g2d.drawImage(p3.getImage(), p3.left, v, null);
		//
		// if (p3.getdx() == -1) {
		//
		// g2d.drawImage(img, 685 - p3.getnX2(), 0, null);
		g2d.drawImage(p3.getImage(), p3.left, v, null);
		// }

		// ro
		g2d.drawImage(ro.getImage(), ro.left, vRo, null);

		if (ro.getdx() == -1) {

			// g2d.drawImage(img, 685 - ro.getnX2(), 0, null);
			g2d.drawImage(ro.getImage(), ro.left, vRo, null);
		}
		ArrayList bullets = Dude.getBullets();
		for (int w = 0; w < bullets.size(); w++) {
			// This is how to get a current element in an arrayList
			// similar to x[2] in a normal array
			Bullet m = (Bullet) bullets.get(w);// draw:
			g2d.drawImage(m.getImage(), m.getX(), m.getY(), 30, 30, null);

		}

		ArrayList bullets2 = Dude2.getBullets();
		for (int w = 0; w < bullets2.size(); w++) {
			// This is how to get a current element in an arrayList
			// similar to x[2] in a normal array
			Bullet m = (Bullet) bullets2.get(w);// draw:
			g2d.drawImage(m.getImage(), m.getX(), m.getY(), 30, 30, null);

		}

		ArrayList bullets3 = Dude3.getBullets();
		for (int w = 0; w < bullets3.size(); w++) {
			// This is how to get a current element in an arrayList
			// similar to x[2] in a normal array
			Bullet m = (Bullet) bullets3.get(w);// draw:
			g2d.drawImage(m.getImage(), m.getX(), m.getY(), 30, 30, null);

		}
		
		g2d.setFont(font);
		g2d.setColor(Color.MAGENTA);
		g2d.drawString("Level : "+ lv, 20, 410);
		g2d.setColor(Color.BLUE);
		g2d.drawString("Eggs:  " + collectCount, 20, 450);
		g2d.setColor(Color.RED);
		g2d.drawString("Shit:  " + shitCount, 20, 480);
		g2d.setColor(Color.DARK_GRAY);
		g2d.drawString("Eggs missed:  " + miss, 20, 510);
		
		 if  (miss  >= 3 || shitCount  >= 3)
		 {
		 g2d.setColor(Color.RED);
		 g2d.drawString("GAME OVER",190,270);
		 time.stop();
		 }
		  if (collectCount >= 20)
		 { g2d.setColor(Color.GREEN);
		 g2d.drawString("YOU WIN",190,270);
		 time.stop();  }
		
		
//		if (miss >= 3 || shitCount >= 3)
//		{
//			g2d.setColor(Color.RED);
//			g2d.drawString("GAME OVER",190,270);
//			time.stop();
//		}
	}

	int collectCount = 0;
	int miss = 0;
	int shitCount = 0;
	int lv =1;
	
	public void checkCollisions() {
		Rectangle r1 = ro.getBounds();
		ArrayList bullets = Dude.getBullets();
		ArrayList bullets2 = Dude2.getBullets();
		ArrayList bullets3 = Dude3.getBullets();
		for (int w = 0; w < bullets.size(); w++) {
			Bullet m = (Bullet) bullets.get(w);
			Rectangle m1 = m.getBounds();
			if (r1.intersects(m1)) {
				System.out.println(m.getImage().toString() + "  ----  "
						+ m.getBounds() + "/" + r1);
				System.out.println("======");
				System.out.println("code1:" + m.getCode1());
				System.out.println("======");
				System.out.println("code2:" + m.getCode2());
				if (m.getCode1() != null)
					collectCount++;
				if (m.getCode2() != null)
					shitCount++;
				System.out.println("Eggs:" + collectCount + "   -   Shit:"
						+ shitCount + "   -   Eggs missed:" + miss);
				m.visible = false;
			} else {
				Rectangle b = new Rectangle(0, 600, 500, 10);
				if (b.intersects(m1) && m.getCode1() != null) {
					// lost = true;
					miss++;
					System.out.println(b + "/" + m.getCode1());
					m.visible = false;
				}
			}

		}

		for (int w = 0; w < bullets2.size(); w++) {
			Bullet m = (Bullet) bullets2.get(w);
			Rectangle m1 = m.getBounds();
			if (r1.intersects(m1)) {
				System.out.println(m.getImage().toString() + "  ----  "
						+ m.getBounds() + "/" + r1);
				System.out.println("======");
				System.out.println("code1:" + m.getCode1());
				System.out.println("======");
				System.out.println("code2:" + m.getCode2());
				if (m.getCode1() != null)
					collectCount++;
				if (m.getCode2() != null)
					shitCount++;
				System.out.println("Eggs:" + collectCount + "   -   Shit:"
						+ shitCount + "   -   Eggs missed:" + miss);
				m.visible = false;
			}
			else {
				Rectangle b = new Rectangle(0, 600, 500, 10);
				if (b.intersects(m1) && m.getCode1() != null) {
					// lost = true;
					miss++;
					System.out.println(b + "/" + m.getCode1());
					m.visible = false;
				}
			}
		}

		for (int w = 0; w < bullets3.size(); w++) {
			Bullet m = (Bullet) bullets3.get(w);
			Rectangle m1 = m.getBounds();
			if (r1.intersects(m1)) {
				System.out.println(m.getImage().toString() + "  ----  "
						+ m.getBounds() + "/" + r1);
				System.out.println("======");
				System.out.println("code1:" + m.getCode1());
				System.out.println("======");
				System.out.println("code2:" + m.getCode2());
				if (m.getCode1() != null)
					collectCount++;
				if (m.getCode2() != null)
					shitCount++;
				System.out.println("Eggs:" + collectCount + "   -   Shit:"
						+ shitCount + "   -   Eggs missed:" + miss);
				m.visible = false;
			}
			else {
				Rectangle b = new Rectangle(0, 600, 500, 10);
				if (b.intersects(m1) && m.getCode1() != null) {
					// lost = true;
					miss++;
					System.out.println(b + "/" + m.getCode1());
					m.visible = false;
				}
			}
		}
		
		
	}

	private class AL extends KeyAdapter {
		public void KeyReleased(KeyEvent e) {
			p.keyReleased(e);
		}

		public void KeyPressed(KeyEvent e) {
			p.keyPressed(e);
		}
	}

	boolean h = false;
	boolean done = false;

	public void cycle() {

		if (h == false)
			v--;
		if (v == 125)
			h = true;
		if (h == true && v <= 180) {
			v++;
			if (v == 180) {
				done = true;
			}
		}
	}

	public void run() {
		long beforeTime, timeDiff, sleep;

		beforeTime = System.currentTimeMillis();

		while (done == false) {

			cycle();

			timeDiff = System.currentTimeMillis() - beforeTime;
			sleep = 10 - timeDiff;

			if (sleep < 0)
				sleep = 2;
			try {
				Thread.sleep(sleep);
			} catch (InterruptedException e) {
				System.out.println("interrupted");
			}

			beforeTime = System.currentTimeMillis();
		}
		done = false;
		h = false;
		done2 = false;
	}
}
